import * as index from '@/api/index.js'
export default {
	data() {
		return {
			menuButtonInfo: uni.getMenuButtonBoundingClientRect(),
			token: '',
			loginForm: {
				code: '',
				mobilecode: ''
			},
			userinfo: {},
			list: [
				// {
				// 	title: '关于我们',
				// 	icon: '/static/home/about.png',
				// 	type: false,
				// 	url: ''
				// },
				{
					title: '我的订单',
					icon: '/static/home/order.png',
					type: true,
					url: '/pages/order/index'
				},
				{
					title: '联系我们',
					icon: '/static/home/call.png',
					type: false,
					url: '',
					mobile:true
				}
			],
			list2: [{
					title: '退出登录',
					icon: '/static/home/logout.png',
					type: false,
					url: ''
				}

			]
		}
	},

	onShow() {
		this.token = uni.getStorageSync('token')
		if (uni.getStorageSync('token')) {
			this.getUserInfo()
		}
	},
	onLoad(option) {
		this.getwxCode()
	},

	methods: {
		jump(item) {
			if(item.mobile){
				index.getKefuPhone().then(res=>{
					uni.makePhoneCall({
						phoneNumber: res.data //仅为示例
					});
				})
			}
			else{
				this.$tools.navigate(item.url, item.type)
			}
		},
		getwxCode() {
			let that = this
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: function(loginRes) {
					that.loginForm.code = loginRes.code
				}
			});
		},
		getPhoneNumber(e) {
			console.log(e,'eee');
			this.loginForm.mobilecode = e.detail.code
			this.wxLogin()
		},
		wxLogin() {
			let that = this
			if (this.loginForm.mobilecode && this.loginForm.code) {
				index.wxmobile(this.loginForm).then(res => {
					// if (res.data.userinfo.avatar == '' || res.data.userinfo.nickname == '') {
					// 	that.$tools.navigate('/pages/user/userinfo/index')
					// }
					uni.setStorage({
						key: 'userInfo',
						data: res.data.userinfo,
						success: function() {
							console.log(666);
							that.userInfo = res.data.userinfo
						}
					})
					uni.setStorage({
						key: 'token',
						data: res.data.userinfo.token,
						success: function() {
							that.getwxCode()
							that.token = res.data.userinfo.token
							that.getUserInfo()

						}
					});
				})
			}
		},
		getUserInfo() {
			let that = this
			index.getUserInfo().then(res => {
				that.userinfo = res.data
				that.token = res.data.token
			})
		},
		logout() {
			let that = this
			uni.showModal({
				title: '提示',
				content: '确定退出登录吗?',
				success: (res) => {
					if (res.confirm) {
						uni.removeStorageSync('token');
						uni.removeStorageSync('userInfo');
						that.token = ''
						that.userInfo = {}
					}
				}
			})
		}
	}
}